package com.burgeon.backend.SHOP.rest;

import com.burgeon.backend.Rest.base.RESTBase;

import com.burgeon.backend.SHOP.entity.FaStorage;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 云仓订单配货库存表
 */
public class QuerryFAStorageYC extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QuerryFAStorageYC(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<FaStorage> getData(String store, String docno, String color, String size) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(store,docno,color,size), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<FaStorage> orderArrayList = new ArrayList<FaStorage>();
        for (int i = 0; i < jsonArray.length(); i++) {
            FaStorage faStorage = new FaStorage();
            faStorage.setId(jsonArray.getJSONArray(i).getString(0));
            faStorage.setcStoreId(jsonArray.getJSONArray(i).getString(1));
            faStorage.setmProductId(jsonArray.getJSONArray(i).getString(2));
            faStorage.setQtycam(jsonArray.getJSONArray(i).getString(3));
            faStorage.setValue1Id(jsonArray.getJSONArray(i).getString(4));
         //   faStorage.setValue1Code(jsonArray.getJSONArray(i).getString(5));
            faStorage.setValue2Id(jsonArray.getJSONArray(i).getString(5));
         //   faStorage.setValue2Code(jsonArray.getJSONArray(i).getString(7));

            orderArrayList.add(faStorage);
        }
        return orderArrayList;
    }

    private MultiValueMap Data(String store,String docno,String color,String size) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");

        columns.add("'C_STORE_ID;NAME'");	////店仓
        columns.add("'M_PRODUCT_ID;NAME'");//	款号
        columns.add(" QTYCAN");//	可配
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE1_ID;NAME'");//颜色
       // columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE1_ID;VALUE'");//颜色
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;NAME'");// 尺寸
       // columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;VALUE'");//尺寸


        params.put("columns", columns);
        params.put("table", "FA_STORAGEYC ");//表名

        params.put("count","true");
        params.put("range",500);

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号

        String ac = "'="+color+"'";
        paramss.put("column", "'M_ATTRIBUTESETINSTANCE_ID;VALUE1_ID;NAME'");
        paramss.put("condition", ac);

//        HashMap expr1= new HashMap();
//        String ac = "'="+docno+"'";
//        expr1.put("column", "'M_PRODUCT_ID;NAME'");
//        expr1.put("condition", ac);
//        paramss.put("expr1",expr1);
//
//        HashMap expr2= new HashMap();
//        String st = "'="+store+"'";
//        expr2.put("column", "'C_STORE_ID;NAME'");
//        expr2.put("condition", st);
//        paramss.put("expr2",expr2);
//        paramss.put("combine","and");


//        HashMap expr1 = new HashMap();
//        HashMap expr1v1= new HashMap();
//        String ac = "'="+docno+"'";
//        expr1v1.put("column", "'M_PRODUCT_ID;NAME'");
//        expr1v1.put("condition", ac);
//        expr1.put("expr1",expr1v1);
//
//        HashMap expr1v2= new HashMap();
//        String st = "'="+store+"'";
//        expr1v2.put("column", "'C_STORE_ID;NAME'");
//        expr1v2.put("condition", st);
//        expr1.put("expr2",expr1v2);
//        expr1.put("combine","and");
//
//
//        HashMap expr2 = new HashMap();
//        HashMap expr2v1 = new HashMap();
//        String c ="'="+color+"'";
//        expr2v1.put("column", "'M_ATTRIBUTESETINSTANCE_ID;VALUE1_ID;NAME'");
//        expr2v1.put("condition",c);
//
//        HashMap expr2v2= new HashMap();
//        String s= "'="+size+"'";
//        expr2v2.put("column","'M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;NAME'");
//        expr2v2.put("condition",s);
//
//        expr2.put("combine","and");
//        expr2.put("expr1",expr2v1);
//        expr2.put("expr2",expr2v2);
//
//        paramss.put("combine", "and");//关系
//        paramss.put("expr1",expr1);
//        paramss.put("expr2",expr2);

        params.put("params", paramss);
        System.err.println(params);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QuerryFAStorageYC querryYCDisStatus = new QuerryFAStorageYC("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(querryYCDisStatus.getData("万达CRZ","CPI4M480","粉红","M"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
